package org.gephi.statistics.spi.path;

import java.io.BufferedWriter;
import java.util.HashSet;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class PathComputingThread implements Runnable {

	Logger logger = LogManager.getLogger();
	LGraph lg;
	HashSet<Integer> startNodeIDSet;
	HashSet<Integer> endNodeIDSet;
	BufferedWriter bufWriter;
	ConcurrentLinkedQueue<String> queue;

	public PathComputingThread(BufferedWriter bufWriter, LGraph lg, HashSet<Integer> startNodeIDSet,
			HashSet<Integer> endNodeIDSet) {
		this.bufWriter = bufWriter;
		this.lg = lg;
		this.startNodeIDSet = startNodeIDSet;
		this.endNodeIDSet = endNodeIDSet;
	}
	
	public PathComputingThread(ConcurrentLinkedQueue<String> queue, LGraph lg, HashSet<Integer> startNodeIDSet,
			HashSet<Integer> endNodeIDSet) {
		this.queue = queue;
		this.lg = lg;
		this.startNodeIDSet = startNodeIDSet;
		this.endNodeIDSet = endNodeIDSet;
	}

	public PathComputingThread(LGraph lg, HashSet<Integer> startNodeIDSet, HashSet<Integer> endNodeIDSet) {
		this.lg = lg;
		this.startNodeIDSet = startNodeIDSet;
		this.endNodeIDSet = endNodeIDSet;
	}

	@Override
	public void run() {
		for (int i : startNodeIDSet) {
			for (int j : endNodeIDSet) {
				LAF operation = new LAF(lg, i, j);
				operation.getResult();
				HashSet<String> tmpStringList = operation.getResultTextList();
				if (tmpStringList != null && tmpStringList.size() != 0) {
					for(String str: tmpStringList){
						logger.info(str);
						synchronized (queue) {
//							this.bufWriter.write(operation.getResultText());
							queue.add(str);
						}
					}
				}
				// try {
				// Thread.sleep(10);
				// } catch (InterruptedException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// }
			}
		}
	}
}
